package com.hddf.project.auth.dao_impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.hddf.project.auth.dao.UserDao;
import com.hddf.project.auth.pojos.User;


public class UserDaoImpl implements UserDao{

	public boolean delete(int id, Session s) {
		try{
			User u = (User)s.get(User.class,id);
			s.delete(u);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public User getUserById(int id, Session s) {
		try{
			User u = (User)s.get(User.class,id);
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public User getUserByName(String name, Session s) {
		try{
			List<User> list = null;
			Query query = s.createQuery("from User as users where users.username=:temp ");
			query.setString("temp",name);
			list = query.list();
			User u = null;
			if(list!=null && list.size()>0){
				u = list.get(0);
			}
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public User getUserByFullName(String fullname, Session s) {
		try{
			List<User> list = null;
			Query query = s.createQuery("from User as users where users.fullname=:temp ");
			query.setString("temp",fullname);
			list = query.list();
			User u = null;
			if(list!=null && list.size()>0){
				u = list.get(0);
			}
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public User getUserByUser_No(String user_no, Session s) {
		try{
			List<User> list = null;
			Query query = s.createQuery("from User as users where users.user_no=:temp ");
			query.setString("temp",user_no);
			list = query.list();
			User u = null;
			if(list!=null && list.size()>0){
				u = list.get(0);
			}
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public boolean save(User user, Session s) {
		try{
			s.save(user);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean update(User user, Session s) {
		try{
			s.update(user);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean setUserOff(int id,Session s) {
		try{
			User u = (User)s.get(User.class,id);
			u.setIsopen(0);
			s.update(u);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean setUserOn(int id,Session s) {
		try{
			User u = (User)s.get(User.class,id);
			u.setIsopen(1);
			s.update(u);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	
}
